<template>
  <div>
    <!-- 面包屑导航 -->

    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">商品管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card class="marginTop">
      <!-- 搜索框 -->
      <el-row class="marginTop">
        <el-col :span="6">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable="true"
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <!-- 添加商品 -->
        <el-col :span="6" style="margin-left:1%;">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品表格信息区域 -->

      <el-row class="marginTop">
        <el-table :data="goodslist" border="true" stripe="true">
          <!-- 索引列 -->
          <el-table-column type="index"></el-table-column>
          <!-- 商品名称 -->
          <el-table-column label="商品名称" prop="goods_name">
          </el-table-column>
          <!-- 商品价格 -->
          <el-table-column label="商品价格" prop="goods_price" width="95px">
          </el-table-column>
          <!-- 商品重量 -->
          <el-table-column label="商品重量" prop="goods_weight" width="95px">
          </el-table-column>
          <!-- 创建时间 -->
          <el-table-column label="创建时间" width="180px">
            <template slot-scope="scope">
              {{ scope.row.add_time | dateFormate }}
            </template>
          </el-table-column>
          <!-- 操作 -->
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-edit" size="mini"
                >修改</el-button
              >
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="removeById(scope.row.goods_id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <!-- 分页 -->
      <el-row class="marginTop">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </el-row>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 请求数据携带的参数
      queryInfo: {
        // 查询关键字
        query: "",
        // 当前页码
        pagenum: 1,
        // 数据大小
        pagesize: 10
      },
      // 商品列表数据
      goodslist: [],
      // 总数据条数
      total: 0
    };
  },
  created() {
    // 调用获取数据的接口
    this.getGoodsList();
  },
  methods: {
    // 获取数据列表
    async getGoodsList() {
      // 发起数据请求
      const { data: res } = await this.$http.get("goods", {
        params: this.queryInfo
      });

      // 判断是否请求成功
      if (res.meta.status !== 200) {
        return this.$message.error("请求商品信息失败！");
      }
      // 请求成功
      this.$message.success("获取商品数据成功！");

      this.goodslist = res.data.goods;
      this.total = res.data.total;
    },
    // 页面数据条数发生改变时
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getGoodsList();
    },
    // 页面数据页码发生变化时
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getGoodsList();
    },
    // 根据id删除数据
    async removeById(id) {
      const confirmResult = await this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).catch(err => err)
      // 取消删除
      if(confirmResult !== "confirm"){
        return this.$message.info('你取消了删除')
      }
      // 调用删除接口
      const {data: res} = await this.$http.delete(`goods/${id}`)
      if(res.meta.status !==200){
        return this.$message.error('删除失败')
      }
      // 删除成功
      this.$message.success('删除成功！')
      this.getGoodsList()
    },
    // 跳转到添加商品页面
    goAddPage(){
      this.$router.push('/goods/add')
    }
  }
};
</script>

<style>
.marginTop {
  margin-top: 1%;
}
</style>